// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use app::Item;
use app::{add,remove,updata};
use app::init_items;
use app::{list_all,list_not_finish,list_finish,list_star};
use chrono::Local;

fn main() {
  init_items();
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![list_item,rm_item,updata_item,add_item])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}


// i: 10当前全部  20已收藏  30已完成
#[tauri::command]
fn list_item(i:u32) -> Vec<Item> {
  match i{
    10 => list_not_finish(),
    20 => list_star(),
    30 => list_finish(),
    _ => list_all()
  }
}

#[tauri::command]
fn rm_item(i: u32) {
  remove(i);
}

#[tauri::command]
fn add_item(id: u32) {
  let mut item = Item::build(id,String::new());
  item.create_time =  Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
  add(item);
}

//todo 把添加删除收藏抽象为更新item
#[tauri::command]
fn updata_item(item :Item) {
//  dbg!("{:?}",item);
  updata(item);
}